<template>
  <div class="aliali-channel-nav">
    <div class="channel-nav-container">
      <div class="channel-scroll-wrapper">
        <div class="channel-scroll" ref="scrollContainer">
          <button
              v-for="channel in channels"
              :key="channel.id"
              class="channel-item"
              :class="{ active: channel.active }"
              @click="handleChannelClick(channel.id)"
          >
            <span class="channel-icon">{{ channel.icon }}</span>
            <span class="channel-name">{{ channel.name }}</span>
          </button>
        </div>

        <!-- 滚动按钮 -->
        <button
            v-if="showScrollButtons"
            class="scroll-btn scroll-prev"
            @click="scrollLeft"
            :disabled="!canScrollLeft"
        >
          ‹
        </button>
        <button
            v-if="showScrollButtons"
            class="scroll-btn scroll-next"
            @click="scrollRight"
            :disabled="!canScrollRight"
        >
          ›
        </button>
      </div>

      <!-- 更多频道 -->
      <div class="channel-more">
        <el-dropdown @command="handleMoreChannel" trigger="click">
<!--          <button class="more-btn">-->
<!--            <span>更多</span>-->
<!--            <el-icon><ArrowDown /></el-icon>-->
<!--          </button>-->
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                  v-for="channel in moreChannels"
                  :key="channel.id"
                  :command="channel.id"
                  :class="{ active: channel.active }"
              >
                <span class="dropdown-icon">{{ channel.icon }}</span>
                {{ channel.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
// import { ArrowDown } from '@element-plus/icons-vue'

const props = defineProps({
  channels: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['channel-change'])

const scrollContainer = ref(null)
const scrollPosition = ref(0)
const containerWidth = ref(0)
const contentWidth = ref(0)

// 计算是否显示滚动按钮
const showScrollButtons = computed(() => {
  return contentWidth.value > containerWidth.value
})

// 计算是否可以向左滚动
const canScrollLeft = computed(() => {
  return scrollPosition.value > 0
})

// 计算是否可以向右滚动
const canScrollRight = computed(() => {
  return scrollPosition.value < contentWidth.value - containerWidth.value
})

// 更多频道（显示在下拉菜单中的频道）
const moreChannels = computed(() => {
  return props.channels.slice(20) // 从第21个频道开始显示在更多中
})

// 处理频道点击
const handleChannelClick = (channelId) => {
  emit('channel-change', channelId)
}

// 处理更多频道点击
const handleMoreChannel = (channelId) => {
  emit('channel-change', channelId)
}

// 滚动控制
const scrollLeft = () => {
  if (scrollContainer.value && canScrollLeft.value) {
    const newPosition = Math.max(0, scrollPosition.value - 200)
    scrollContainer.value.scrollTo({
      left: newPosition,
      behavior: 'smooth'
    })
    scrollPosition.value = newPosition
  }
}

const scrollRight = () => {
  if (scrollContainer.value && canScrollRight.value) {
    const newPosition = Math.min(
        contentWidth.value - containerWidth.value,
        scrollPosition.value + 200
    )
    scrollContainer.value.scrollTo({
      left: newPosition,
      behavior: 'smooth'
    })
    scrollPosition.value = newPosition
  }
}

// 更新尺寸信息
const updateDimensions = () => {
  if (scrollContainer.value) {
    containerWidth.value = scrollContainer.value.clientWidth
    contentWidth.value = scrollContainer.value.scrollWidth
  }
}

// 处理滚动事件
const handleScroll = () => {
  if (scrollContainer.value) {
    scrollPosition.value = scrollContainer.value.scrollLeft
  }
}

onMounted(() => {
  updateDimensions()
  window.addEventListener('resize', updateDimensions)
  if (scrollContainer.value) {
    scrollContainer.value.addEventListener('scroll', handleScroll)
  }
})

onUnmounted(() => {
  window.removeEventListener('resize', updateDimensions)
  if (scrollContainer.value) {
    scrollContainer.value.removeEventListener('scroll', handleScroll)
  }
})
</script>

<style scoped>
.aliali-channel-nav {
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.channel-nav-container {
  display: flex;
  align-items: center;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

.channel-scroll-wrapper {
  position: relative;
  flex: 1;
  overflow: hidden;
}

.channel-scroll {
  display: flex;
  gap: 0;
  padding: 12px 0;
  overflow-x: auto;
  scroll-behavior: smooth;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.channel-scroll::-webkit-scrollbar {
  display: none;
}

.channel-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 16px;
  border: none;
  background: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 6px;
  white-space: nowrap;
  flex-shrink: 0;
  min-width: 60px;
}

.channel-item:hover {
  background: #f5f7fa;
  color: #00a1d6;
}

.channel-item.active {
  background: #00a1d6;
  color: white;
}

.channel-icon {
  font-size: 18px;
  margin-bottom: 4px;
  transition: transform 0.3s ease;
}

.channel-item:hover .channel-icon {
  transform: scale(1.1);
}

.channel-name {
  font-size: 12px;
  font-weight: 500;
}

/* 滚动按钮 */
.scroll-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.scroll-btn:hover:not(:disabled) {
  background: #00a1d6;
  color: white;
  transform: translateY(-50%) scale(1.1);
}

.scroll-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.scroll-prev {
  left: 0;
}

.scroll-next {
  right: 0;
}

/* 更多按钮 */
.channel-more {
  flex-shrink: 0;
  margin-left: 12px;
}

.more-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  border: 1px solid #f0f0f0;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 12px;
  color: #61666d;
}

.more-btn:hover {
  border-color: #00a1d6;
  color: #00a1d6;
}

/* 下拉菜单样式 */
:deep(.el-dropdown-menu) {
  max-height: 300px;
  overflow-y: auto;
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
}

.dropdown-icon {
  font-size: 14px;
}

:deep(.el-dropdown-menu__item.active) {
  color: #00a1d6;
  background: #f0f9ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .channel-nav-container {
    padding: 0 12px;
  }

  .channel-item {
    padding: 6px 12px;
    min-width: 50px;
  }

  .channel-icon {
    font-size: 16px;
  }

  .channel-name {
    font-size: 10px;
  }

  .scroll-btn {
    display: none;
  }
}
</style>